<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
       
        function fn2(m, n) {
            let arr = [];
            for (let i = 1; i <= m * n; i++) {
                let row = Math.ceil(i / n) - 1;
                let col = i % n === 0 ? n - 1 : (i - 1) % n;
                if (!arr[row]) arr[row] = [];
                arr[row][col] = i;
            }

            for (let i = 0; i < arr.length; i++) {
                for (let j = 0; j < arr[i].length; j++) {
                    let index = j + i * n;
                    if (index >= m * n) break;
                    let num = index + 1;
                    arr[i][j] = num;
                }
            }

            for (let i = 0; i < arr.length; i++) {
                for (let j = 0; j < arr[i].length; j++) {
                    let num = arr[i][j];
                    let row = Math.ceil(num / n) - 1;
                    let col = num % n === 0 ? n - 1 : (num - 1) % n;
                    let newRow = col;
                    let newCol = m - row - 1;
                    arr[newRow][newCol] = num;
                }
            }

            for (let i = 0; i < arr.length; i++) {
                console.log(arr[i].join("  "));
            }
        }

        fn2(5, 7);
    </script>

</body>

</html>